package com.gdk.ksoft.assembly.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * Created by Mrper on 2014/11/25.
 * 进度条
 */
public class ProgressBar extends View {

    private Paint paint = new Paint();
    private int progressBarHeight = 20;
    private int progress = 0;
    private int min = 0;
    private int max = 100;
    private int width = 0;//height = 0;
//    private int circleRadius = 90;


    public void setMin(int min){
        if(min<0 || min>max)return;
        this.min  = min;
        this.invalidate();
    }

    public int getMin(){
        return this.min;
    }

    public void setMax(int max){
        if(max<=0 || max<min) return;
        this.max = max;
        this.invalidate();
    }

    public int getMax(){
        return this.max;
    }

    public void setProgress(int progress){
        if(progress<min || progress>max)return;
        this.progress = progress;
        this.invalidate();
    }

    public int getProgress(){
        return this.progress;
    }

    public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public ProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ProgressBar(Context context) {
        super(context);
        init(context);
    }

    public void setProgressBarHeight(int progressBarHeight){
        if(progressBarHeight<=0) progressBarHeight = 10;
        this.progressBarHeight = progressBarHeight;
        this.invalidate();
    }

    public int getProgressBarHeight(){
        return this.progressBarHeight;
    }

    public void init(Context context){
        progressBarHeight = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,25f,getResources().getDisplayMetrics());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        width = getMeasuredWidth();
//        height = getMeasuredHeight();
        paint.reset();
        paint.setColor(Color.DKGRAY);
        canvas.drawRoundRect(new RectF(0, 0, width, progressBarHeight), 5f, 5f, paint);
        paint.setColor(Color.LTGRAY);
        canvas.drawRoundRect(new RectF(0, 0,(int)(((float)progress/max)*(float)width), progressBarHeight), 5f, 5f, paint);
 //       canvas.drawArc(new RectF(0,0,circleRadius,circleRadius),0,360,false,paint);
//        canvas.drawCircle(circleRadius/2,circleRadius/2,circleRadius,paint);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(progressBarHeight,MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
